package com.zhiwei.most.views

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.ColorInt
import androidx.appcompat.widget.AppCompatTextView
import com.blankj.utilcode.util.ResourceUtils
import com.blankj.utilcode.util.SizeUtils
import com.zhiwei.most.R
import com.zhiwei.most.ext.onClickRxView

/**
 * Create by zhiweizhu on 2022/2/28
 */
class WatchButton @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {


    private var mWatchDrawable: Drawable
    private var mWatchText: String
    private var mWatchTextColor: Int
    private var mWatchTextSize: Int
    private var mWatchTextBold: Boolean

    private var mTextView: AppCompatTextView

    private var mBlockCall: ((View) -> Unit)? = null


    init {
        val typedValue = context.obtainStyledAttributes(attrs, R.styleable.WatchButton)
        //图片
        mWatchDrawable = typedValue.getDrawable(R.styleable.WatchButton_wbDrawable) ?: ResourceUtils.getDrawable(R.drawable.ic_btn_moon_yellow)
        //文字
        mWatchText = typedValue.getString(R.styleable.WatchButton_wbText) ?: ""
        //文字颜色
        mWatchTextColor = typedValue.getColor(R.styleable.WatchButton_wbTextColor, Color.WHITE)
        //文字大小
        mWatchTextSize = typedValue.getDimensionPixelSize(R.styleable.WatchButton_wbTextSize, SizeUtils.dp2px(23f))
        //文字是否加粗
        mWatchTextBold = typedValue.getBoolean(R.styleable.WatchButton_wbTextBold, false)
        typedValue.recycle()

        mTextView = AppCompatTextView(context).apply {
            layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
                gravity = Gravity.CENTER
            }
            text = mWatchText
            setTextColor(mWatchTextColor)
            textSize = mWatchTextSize.toFloat()
            paint.isFakeBoldText = mWatchTextBold
        }
        background = mWatchDrawable
        addView(mTextView)

        mTextView.onClickRxView {
            mBlockCall?.invoke(it)
        }
    }

    fun setWatchButtonText(str: String){
        mTextView.text = str
    }

    fun setWatchButtonTextColor(@ColorInt color: Int){
        mTextView.setTextColor(color)
    }

    fun setWatchButtonTextBold(bold: Boolean){
        mTextView.paint.isFakeBoldText = bold
    }

    fun setWatchButtonTextSize(textSize: Int){
        mTextView.textSize = textSize.toFloat()
    }

    fun setOnClickListener(block : (View) -> Unit){
        mBlockCall = block
    }

}